package com.pandai.leetcode;

public class Solution209 {
    /**
     *      双指针-滑动窗口
     * @param target
     * @param nums
     * @return
     */
    public int minSubArrayLen(int target, int[] nums) {
        int i = 0;
        int sum=0;
        int minLen = Integer.MAX_VALUE;
        // 滑动窗口（双指针思想），i代表起点，j代表终点，sum代表区间内容的元素和
        for(int j = 0;j<nums.length;j++){
            sum+=nums[j];
            while(sum>=target){
                if(minLen > j-i+1){
                    minLen = j-i+1;
                }
                sum-=nums[i];
                i++;
            }
        }
        if(minLen == Integer.MAX_VALUE){
            return 0;
        }
        return minLen;
    }

    public static void main(String[] args) {
        Solution209 solution = new Solution209();
        solution.minSubArrayLen(7,new int[]{2,3,1,2,4,3});
    }
}
